package com.bicon.botu.mall.member.service.impl;

/**
 * 收货地址的实现类
 *
 * @Author 李双喜
 * @Date 2017/7/19 17:26.
 */

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.member.common.MemberConstants;
import com.bicon.botu.mall.member.dto.DefaultAddressDto;
import com.bicon.botu.mall.member.entity.Address;
import com.bicon.botu.mall.member.mapper.AddressMapper;
import com.bicon.botu.mall.member.service.IAddressService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;

    /**
     * 修改收货地址
     *
     * @param address 地址对象
     * @return
     */
    @Override
    public BaseResult updateAddress(Address address) {
        Integer i = addressMapper.updateById(address);//保存放在service
        if (i > 0) {
            return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200);
        }
        return new BaseResult(Constants.RESPONSE_CODE_600, Constants.RESPONSE_MESSAGE_600);
    }

    /**
     * 新增收货地址
     *
     * @param address 地址对象
     * @return
     */
    @Override
    public boolean addAddress(Address address) {
        //判断新增地址是否为默认地址
        if (address.getAddressDefaultChoose().equals(MemberConstants.ADDRESS_DEFAULT_NORMAL)) {
            //根据用户id查出数据库中的默认地址
            Address entity = this.selectOne(new EntityWrapper<Address>().eq("user_id", address.getUserId())
                    .eq("logic_delete", MemberConstants.DEL_LOGIC_NORMAL).eq("address_default_choose", MemberConstants.ADDRESS_DEFAULT_NORMAL));
            //修改数据库中的默认地址
            if (null != entity) {
                entity.setAddressDefaultChoose(MemberConstants.ADDRESS_DEFAULT_NONE);
                entity.preUpdate();
                entity.updateById();
            }
        }
        address.preInsert();
        return address.insert();
    }


    /**
     * 逻辑删除收货地址
     */
    @Override
    public boolean deleteAddress(String id) {
        //先查
        Address address = addressMapper.selectById(id);
        address.setLogicDelete(MemberConstants.DEL_LOGIC_DELETE);
        address.preUpdate();
        address.updateById();
        return address.updateById();
    }


    /**
     * 查询默认收货地址
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public DefaultAddressDto selectDefaultAddress(String userId) {
        DefaultAddressDto defaultAddressDto = new DefaultAddressDto();
        Address address = this.selectOne(new EntityWrapper<Address>().eq("user_id", userId)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL).eq("address_default_choose", "0"));
        if (null != address) {
            defaultAddressDto.setId(address.getId());
            defaultAddressDto.setProvince(address.getAddressProvince());
            defaultAddressDto.setCity(address.getAddressCity());
            if (StringUtils.isNotEmpty(address.getAddressZip())) {
                defaultAddressDto.setAddressZip(address.getAddressZip());
            }
            if (StringUtils.isNotEmpty(address.getAddressDetailed())) {
                defaultAddressDto.setAddressDetailed(address.getAddressDetailed());
            }
            if (StringUtils.isNotEmpty(address.getAddressArea())) {
                defaultAddressDto.setArea(address.getAddressArea());
                defaultAddressDto.setAddressLocation(address.getAddressProvince() + address.getAddressCity() + address.getAddressArea() + address.getAddressDetailed());
            } else {
                defaultAddressDto.setAddressLocation(address.getAddressProvince() + address.getAddressCity() + address.getAddressDetailed());
            }
            defaultAddressDto.setAddressName(address.getAddressName());
            defaultAddressDto.setAddressPhone(address.getAddressPhone());
        }
        return defaultAddressDto;
    }

    /**
     * 查询多个地址
     *
     * @param addressIds 多个地址id
     * @return
     */
    @Override
    public List<Address> selectAddress(String[] addressIds) {
        List<Address> addressList = this.selectList(new EntityWrapper<Address>()
                .eq("logic_delete",Constants.DEL_LOGIC_NORMAL).in("id",addressIds));
        return addressList;
    }


}
